<?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;

use yii\helpers\Html;

use common\helpers\Test;

/**
 * 数据库操作测试类
 */
class DbController extends Controller
{
    //设置独立控制器
    public function actions()
    {
        return [
            // 用类来申明操作成功
            //'test' => 'frontend\components\HelloAction',
    
            // 用配置数组申明 "view" 动作
//             'view' => [
//                 'class' => 'yii\web\ViewAction',
//                 'viewPrefix' => '',
//             ],
        ];
    }
    
    //过滤器的作用，有前过滤和后过滤
    public function behaviors()
    {
        return [
            [
                //确实执行了
                'class' => 'frontend\components\ActionTimeFilter',
                'only' => ['index', 'view'],  //过滤器只是用在那些操作上
                'except'=>[],//过滤器不使用在那些操作上
                /* 'lastModified' => function ($action, $params) {
                    $q = new \yii\db\Query();
                    return $q->from('user')->max('updated_at');
                }, */
                ],
                ];
    }
    
    
    public function actionIndex()
    {
        
//         var_dump(Yii::$app->db);

        //执行sql
        $posts = Yii::$app->db->createCommand('SELECT * FROM me_articles')->queryAll();
        $post = Yii::$app->db->createCommand('SELECT * FROM me_articles WHERE id=1')->queryOne();
        //返回一个标量
        $count = Yii::$app->db->createCommand('SELECT COUNT(*) FROM me_articles')->queryScalar();
        
        $post = Yii::$app->db->createCommand('SELECT * FROM me_articles WHERE id=:id AND is_public=:is_public')
        ->bindValue(':id', 2)
        ->bindValue(':is_public', 1)
        ->queryOne();
        
        var_dump($post);
        
    }
    
    
    
    
    
    
    
    
    
    
    
}
